using System;
using System.Collections;
using UnityEngine;

public class PullButton : MonoBehaviour
{
	public delegate void OnPressed(bool pressed);

	public OnPressed onPressed;

	public Action onClick;

	public UIWidget target;

	public UIWidget container;

	public float returnSpeed = 5f;

	private Vector3 originalLocalPosition;

	private bool buttonPressed;

	private void Awake()
	{
		if (target == null)
		{
			target = base.gameObject.GetComponent<UIWidget>();
		}
		originalLocalPosition = target.cachedTransform.localPosition;
	}

	private void OnPress(bool pressed)
	{
		StopAllCoroutines();
		if (!pressed)
		{
			if (buttonPressed)
			{
				if (onClick != null)
				{
					onClick();
				}
			}
			else if (onPressed != null)
			{
				onPressed(false);
			}
		}
		buttonPressed = false;
		if (!pressed)
		{
			StartCoroutine(DoReturnToOriginalPosition());
		}
	}

	private IEnumerator DoReturnToOriginalPosition()
	{
		while (true)
		{
			target.cachedTransform.localPosition = Vector3.Lerp(target.cachedTransform.localPosition, originalLocalPosition, Time.deltaTime * returnSpeed);
			yield return null;
		}
	}

	private void OnDrag(Vector2 delta)
	{
		StopAllCoroutines();
		Vector3 vector = UICamera.currentCamera.ScreenToWorldPoint(UICamera.currentTouch.pos);
		Vector3 position = target.cachedTransform.position;
		Vector3[] worldCorners = container.worldCorners;
		Vector3 vector2 = worldCorners[0];
		Vector3 vector3 = worldCorners[1];
		position.y = Mathf.Clamp(vector.y, Mathf.Min(vector2.y, vector3.y), Mathf.Max(vector2.y, vector3.y));
		float num = Mathf.Abs(vector3.y - vector2.y);
		float num2 = Mathf.Abs(position.y - vector2.y);
		bool flag = buttonPressed;
		buttonPressed = num2 <= num * 0.5f;
		if (flag != buttonPressed && onPressed != null)
		{
			onPressed(buttonPressed);
		}
		target.cachedTransform.position = position;
	}
}
